//
//  HomeViewController.swift
//  iWatch
//
//  Created by 时空矩人 on 16/4/9.
//  Copyright © 2016年 时空矩人. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {
    
    private var flag: Int = -1
    private var headView: HomeTableHeadView?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        
        view.backgroundColor = UIColor.grayColor()
        
        addNavBarBtn()
        
        buildTableHeadView()
        
        
    }
    
    //无线轮播
    private func buildTableHeadView() {
        
        headView = HomeTableHeadView()
        
        headView?.delegate = self
        weak var tmpSelf = self
        
        HeadResources.loadHomeHeadData { (data, error) -> Void in
            if error == nil {
                tmpSelf?.headView?.headData = data
                tmpSelf?.headData = data
                tmpSelf?.collectionView.reloadData()
            }
        }
        
        collectionView.addSubview(headView!)
        FreshHot.loadFreshHotData { (data, error) -> Void in
            tmpSelf?.freshHot = data
            tmpSelf?.collectionView.reloadData()
            tmpSelf?.collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 64, right: 0)
        }
        
    }
    
    func addNavBarBtn() {
        
        //navigationItem.rightBarButtonItem
        let rightBarBtn = UIButton()
        rightBarBtn.setImage(UIImage(named: "home_icon_consult_white.png"), forState: .Normal)
        rightBarBtn.setImage(UIImage(named: "home_icon_consult"), forState: .Highlighted)
        //        rightBarBtn.sizeToFit()
        rightBarBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        rightBarBtn.addTarget(self, action: #selector(HomeViewController.rightBarBtnDidSelected), forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightBarBtn)
        
        
        //navigationItem.leftBarButtonItem
        let leftBarBtn = UIButton()
        leftBarBtn.setImage(UIImage(named: "home_icon_sweep_white.png"), forState: .Normal)
        leftBarBtn.setImage(UIImage(named: "home_icon_sweep.png"), forState: .Highlighted)
        //        leftBarBtn.sizeToFit()
        leftBarBtn.frame = CGRect(x: 0, y: 0, width: 44, height: 44)
        leftBarBtn.addTarget(self, action: #selector(HomeViewController.leftBarBtnDidSelected), forControlEvents: UIControlEvents.TouchUpInside)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftBarBtn)
    }
    
    //右边navigationItem的按钮触发的事件
    func rightBarBtnDidSelected() {
        print("按爆了按爆了")
        
    }
    
    //右边navigationItem的按钮触发的事件
    func leftBarBtnDidSelected() {
        
        let scanQRCodeVC = ScanQRCodeVC()
        
        self.navigationController?.pushViewController(scanQRCodeVC, animated: true)
        print("按了按了")
    }

}
